﻿using DotNetStar.TheKing.Infrastructure;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;

namespace DotNetStar.TheKing.Web.TagHelpers
{
    [HtmlTargetElement("exp-pregress",TagStructure = TagStructure.WithoutEndTag)]
    public class ExpProgressBarTagHelper:TagHelper
    {
        public long Exp { get; set; }
        public long ExpMax { get; set; }

        public BootstrapColors Color { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (this.ExpMax <= 0)
            {
                this.ExpMax = 1;
            }
            var percentage =Math.Floor(((double)this.Exp / this.ExpMax * 100)).ToString("f0");
            string template = @"
  <div class='progress-bar{0}' role='progressbar' aria-valuenow='{1}' aria-valuemin='0' aria-valuemax='100' 
style='width: {1}%;min-width: 2em;' title='{2}/{3}'>
    {1}%
  </div>
            ".StringFormat(this.Color== BootstrapColors.Primary?string.Empty: $" bg-{this.Color.GetDescription()}"
            , percentage,this.Exp,this.ExpMax
            )
;
            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.Add("class", "progress");
            output.Content.SetHtmlContent(template);
        }
    }
}
